const express = require('express');
const bodyParser = require('body-parser');
const authRoutes = require('./routes/auth.route');
const apiRoutes = require('./routes/api.route');
const { sequelize } = require('./config/database');

const app = express();

// 中间件
app.use(bodyParser.json());

// 数据库连接
sequelize.sync()
  .then(() => console.log('Database connected'))
  .catch(err => console.error('Database connection error:', err));

// 路由
app.use('/auth', authRoutes);
app.use('/api', apiRoutes);

// 错误处理
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).json({
    success: false,
    message: 'Internal server error'
  });
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});